package net.caixiaomi.info.Lottery.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.Lottery.callback.LotteryBallBettingCallBack;
import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.Lottery.utils.RandomUtils;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.MatchItem;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

/**
 * Created by suntian on 2018/8/15.
 */

public class LotteryBettingPresenter {

    private Context mContext;
    private int mTrebleNum;
    private boolean mAddChips;
    private int mChips;
    private String mMoney;

    private LotteryBallBettingCallBack mCallBack;


    private HashMap<String,String> randomMap=new HashMap<>();
    private List<LotteryBettingEntity> mRandomList=new ArrayList<>();


    public LotteryBettingPresenter(Context context,LotteryBallBettingCallBack callBack) {
        this.mContext = context;
        mCallBack=callBack;
    }

    public List<LotteryBettingEntity> getRandomCount(int count){
        randomMap.clear();
        mRandomList.clear();
        while (count>0){
            getRandom();
            count--;
        }
        return mRandomList;
    }

    private void getRandom(){
        String bet="";
        int befores[]= RandomUtils.getRandomNum(35,1,5);
        int afters[]=RandomUtils.getRandomNum(12,1,2);
        for(int m=0;m<befores.length;m++){
            bet=bet+befores[m]+"-";
        }
        for(int m=0;m<afters.length;m++){
            bet=bet+afters[m]+"-";
        }
        if(TextUtils.isEmpty(randomMap.get(bet))){
            randomMap.put(bet,"result");
            LotteryBettingEntity entity=new LotteryBettingEntity();
            entity.setMoney(2);
            entity.setChips(1);
            entity.setStatus(0);
            List<String> beforeList=new ArrayList<>();
            List<String> afterList=new ArrayList<>();
            for(int m=0;m<befores.length;m++){
                if(befores[m]<10){
                    beforeList.add("0"+befores[m]);
                }else{
                    beforeList.add(""+befores[m]);
                }
            }
            for(int m=0;m<afters.length;m++){
                if(afters[m]<10){
                    afterList.add("0"+afters[m]);
                }else{
                    afterList.add(""+afters[m]);
                }
            }
            entity.setmBefores(beforeList);
            entity.setmAfters(afterList);
            mRandomList.add(entity);
        }else{
            getRandom();
        }
    }



    public void setParams(int mTrebleNum,boolean mAddChips){
        this.mTrebleNum=mTrebleNum;
        this.mAddChips=mAddChips;
    }

    public String getTotalDes(){
        String des="";
        mChips=0;
        long money=0;
        for(int i=0;i<CommonApp.mLotteryBettingList.size();i++){
            LotteryBettingEntity entity=CommonApp.mLotteryBettingList.get(i);
            mChips=mChips+entity.getChips();
            money=money+entity.getMoney();
        }
        money= (long) (money*mTrebleNum*(mAddChips?1.5:1));
        mMoney=money+"";
        des="<html><body>"+mChips+"注"+mTrebleNum+"倍  共需: ¥<font color='red'>"+money+"</font>元</body></html>";
        return des;
    }

    private JSONArray getListParams(){
        JSONArray lotteryBetPlays = new JSONArray();
        for(int i=0;i<CommonApp.mLotteryBettingList.size();i++){
            LotteryBettingEntity entity=CommonApp.mLotteryBettingList.get(i);
            JSONObject lotteryBetCell = new JSONObject();
            int amount= (int) (entity.getMoney()*mTrebleNum*(mAddChips?1.5:1));
            lotteryBetCell.put("amount",amount+"");
            lotteryBetCell.put("betNum",entity.getChips());
            lotteryBetCell.put("playType",entity.getStatus());
            String betInfo="";
            switch (entity.getStatus()){
                case 0:
                case 1:
                    for(int j=0;j<entity.getmBefores().size();j++){
                        betInfo=betInfo+entity.getmBefores().get(j);
                        if(j==entity.getmBefores().size()-1){
                            betInfo=betInfo+"|";
                        }else{
                            betInfo=betInfo+",";
                        }
                    }
                    for(int j=0;j<entity.getmAfters().size();j++){
                        betInfo=betInfo+entity.getmAfters().get(j);
                        if(j!=entity.getmAfters().size()-1){
                            betInfo=betInfo+",";
                        }
                    }
                    break;
                case 2:
                    for(int j=0;j<entity.getmBeforeDans().size();j++){
                        betInfo=betInfo+entity.getmBeforeDans().get(j);
                        if(j==entity.getmBeforeDans().size()-1){
                            betInfo=betInfo+"$";
                        }else{
                            betInfo=betInfo+",";
                        }
                    }
                    for(int j=0;j<entity.getmBeforeTuos().size();j++){
                        betInfo=betInfo+entity.getmBeforeTuos().get(j);
                        if(j==entity.getmBeforeTuos().size()-1){
                            betInfo=betInfo+"|";
                        }else{
                            betInfo=betInfo+",";
                        }
                    }
                    for(int j=0;j<entity.getmAfterDans().size();j++){
                        betInfo=betInfo+entity.getmAfterDans().get(j);
                        if(j==entity.getmAfterDans().size()-1){
                            betInfo=betInfo+"$";
                        }else{
                            betInfo=betInfo+",";
                        }
                    }
                    for(int j=0;j<entity.getmAfterTuos().size();j++){
                        betInfo=betInfo+entity.getmAfterTuos().get(j);
                        betInfo=betInfo+",";
//                        if(j==entity.getmAfterTuos().size()-1){
//                            betInfo=betInfo+"$";
//                        }else{
//                            betInfo=betInfo+",";
//                        }
                    }
                    break;
            }
            lotteryBetCell.put("betInfo",betInfo);
            lotteryBetPlays.add(lotteryBetCell);
        }

        return lotteryBetPlays;
    }

    public void betting(){
        JSONObject params = new JSONObject();
        params.put("betNum", mChips); //投注注数
        params.put("bonusId", "");  //用户红包id,如果没有不填写，可以为空 ,
        params.put("isAppend", mAddChips?1:0);   //是否追加，0否1是
        params.put("orderMoney", mMoney);   //投注金额
        params.put("times", mTrebleNum);  //投注倍数
        params.put("betInfos", getListParams());

        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel> confirm = requestService.saveLotteryBetInfo(params);
        confirm.enqueue(new ResponseCallback<BaseCallModel>() {
            @Override
            public void onSuccess(BaseCallModel data) {
                String patToken= (String) data.data;
                mCallBack.betSUccess(patToken);
            }

            @Override
            public void onFail(ResponseError error) {
                ToastUtil.s(error.getMessage());
            }

            @Override
            public void onError() {

            }
        });
    }
}
